package org.example.day02;

/**
 * 第一章节1.8.2:suspend和resume方法的缺点
 */
public class MyThread extends Thread{
    synchronized public void printString(){
        System.out.println("begin");
        if(Thread.currentThread().getName().equals("a")){
            System.out.println("a 线程永远 suspend 了");
            Thread.currentThread().suspend();
        }
        System.out.println("end");
    }

    public static void main(String[] args) {
        try {
            final MyThread object = new MyThread();
            Thread t1 = new Thread(){
                @Override
                public void run() {
                    object.printString();
                }
            };
            t1.setName("a");
            t1.start();
            Thread.sleep(1000);
            Thread t2 = new Thread(){
                @Override
                public void run() {
                    System.out.println("t2启动了，但是进不去printString方法");
                    System.out.println("printString()被a线程占用，且a线程暂停了");
                    object.printString();
                }
            };
            t2.start();
            Thread.sleep(1000);
            System.out.println("t2.isAlive(): " + t2.isAlive()); //true
            System.out.println("t1.isAlive(): " + t1.isAlive()); //true
            System.out.println(t1.getState()); //RUNNABLE
            System.out.println(t2.getState()); //BLOCKED
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
